<template>
  <div class="app-container">
    <div class="filterDiv">
      <el-input v-model="listQuery.referCoaName" size="small" placeholder="科目对照代码/名称" style="width: 200px;" class="filter-item" />
      <el-button v-permiss="['60103620']" size="small" type="primary" round @click="handleFilter">查询</el-button>
    </div>
    <div class="contentDiv">
      <el-table :key="tableKey" v-loading="listLoading" :data="tableData" fit highlight-current-row style="width: 100%;" size="small">
        <el-empty slot="empty" :image-size="120" description="暂无数据" />
        <el-table-column label="科目对照代码" align="center" width="110">
          <template slot-scope="{row}">
            <span>{{ row.referCoaCode }}</span>
          </template>
        </el-table-column>
        <el-table-column label="科目对照名称" align="center">
          <template slot-scope="{row}">
            <span>{{ row.referCoaName }}</span>
          </template>
        </el-table-column>
        <el-table-column label="科目代码" align="center">
          <template slot-scope="{row}">
            <span>{{ row.coaCode }}</span>
          </template>
        </el-table-column>
        <el-table-column label="科目名称" align="center">
          <template slot-scope="{row}">
            <span>{{ row.coaName }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="100">
          <template slot-scope="{row}">
            <el-button v-permiss="['60103630']" type="text" size="small" @click="handleCompile(row)">编辑</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-dialog :close-on-click-modal="false" title="修改科目对照" :visible.sync="dialogVisible" width="460px">
      <el-form ref="dataForm" :rules="rules" :model="form" label-position="right" label-width="110px" style="width: 400px; margin-left:10px;">
        <el-form-item label="科目对照代码" prop="referCoaCode">
          <el-input v-model="form.referCoaCode" disabled placeholder="科目对照代码" />
        </el-form-item>
        <el-form-item label="科目对照名称" prop="referCoaName">
          <el-input v-model="form.referCoaName" disabled placeholder="科目对照名称" />
        </el-form-item>
        <el-form-item label="科目代码" prop="coaCode">
          <el-input v-model="form.coaCode" placeholder="科目代码" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" align="center">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleModify()">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { getReferCoa, saveReferCoa } from '@/api/basedata'
export default {
  name: 'BaseReferCoa',
  data() {
    return {
      tableKey: 0,
      tableData: [],
      listLoading: true,
      listQuery: {
        page: 1,
        referCoaName: ''
      },
      form: {
        id: '',
        referCoaName: '',
        referCoaCode: '',
        coaCode: ''
      },
      resetform: {
        id: '',
        referCoaName: '',
        referCoaCode: '',
        coaCode: ''
      },
      dialogVisible: false,
      rules: {
        referCoaName: [{ required: true, message: '名称不能为空', trigger: 'change' }],
        referCoaCode: [{ required: true, message: '代码不能为空', trigger: 'change' }],
        priceRatio: [{ required: true, message: '价格比不能为空', trigger: 'change' }],
        coaCode: [{ required: true, message: '对应科目代码不能为空', trigger: 'change' }],
        isDisable: [{ required: false }]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      getReferCoa(this.listQuery).then(response => {
        this.listLoading = false
        this.tableData = response.data.data
      }).catch(() => {
        this.listLoading = false
      })
    },
    handleCompile(obj) {
      this.dialogVisible = true
      for (var key in this.form) {
        this.form[key] = obj[key]
      }
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleModify() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          saveReferCoa(this.form).then(res => {
            if (res.data.errorCode == 0) {
              this.getList()
              this.dialogVisible = false
              this.$message.success(res.data.msg)
            }
          })
        }
      })
    }
  }
}
</script>
